package common.entities.abst;


/**
 * @author Yoyo
 *
 */
public class AbstNounEntity extends AbstPersistentEntity {
	private static int nameMinLength = 1;
	private static int nameMaxLength = 45;
	
	private String name;
	
	
	/**
	 * This constructor will be used for entities that are fetched from the DB
	 * 
	 * @param id
	 * @param name
	 */
	public AbstNounEntity(int id, String name) {
		super(id);
		
		this.name = fixEscapeCharacters(name);
	}
	

	/**
	 * This constructor will be used for new entities (i.e still weren't saved into DB)
	 * 
	 * @param name
	 */
	public AbstNounEntity(String name) {
		super();
		
		this.name = fixEscapeCharacters(name);
			
	}

	public AbstNounEntity() {
		this(null);		
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return fixEscapeCharactersReverse(name);
	}
	
	public String escapedGetName() {
		return name;
	}

	
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = fixEscapeCharacters(name);
	}

	/**
	 * Is used to check the restrictions of the entity. 
	 * @return true if it's fields violate the restrictions, and false otherwise
	 */
	public boolean isViolateRestrictions() {
		if(this.getName() == null)
			return true;
		else if(this.getName().length() <= AbstNounEntity.nameMinLength ||
				this.getName().length() >= AbstNounEntity.nameMaxLength)
			return true;
		
		return false;
	}

	/**
	 * Is used to validate the name according to the DB constraints
	 * 
	 * @param name
	 * @throws IllegalArgumentException
	 */
	public static void validateName(String name) throws IllegalArgumentException{
		if(name == null)
			throw new IllegalArgumentException("Name cannot be null");
		else if(name.length() < AbstNounEntity.nameMinLength)
			throw new IllegalArgumentException(String.format("Name cannot have less than %s characters", AbstNounEntity.nameMinLength));
		else if(name.length() > nameMaxLength)
			throw new IllegalArgumentException(String.format("Name cannot have more than %s characters", AbstNounEntity.nameMaxLength));
		
	}

}
